<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <!-- 引入本地 jQuery 库 -->
    <script src="./javascript/jquery-3.3.1.min.js"></script>
    <style>
        /* 可选：添加一些基本的样式 */
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        form {
            max-width: 400px;
            margin: auto;
        }
        label {
            display: block;
            margin-bottom: 8px;
        }
        input {
            width: 100%;
            padding: 8px;
            margin-bottom: 10px;
        }
        button {
            padding: 10px 15px;
        }
    </style>
</head>
<body>
<h1>注册</h1>
<form id="registerForm">
    <label for="name">姓名：</label>
    <input type="text" id="name" name="name" required>

    <label for="pwd">密码：</label>
    <input type="password" id="pwd" name="pwd" required>

    <label for="phone">电话：</label>
    <input type="tel" id="phone" name="phone" required>

    <button type="submit">注册</button>
</form>

<!-- jQuery 代码 -->
<script>
    $(document).ready(function() {
        $('#registerForm').on('submit', function(event) {
            event.preventDefault(); // 防止表单的默认提交行为

            var name = $('#name').val();
            var password = $('#pwd').val();
            var phone = $('#phone').val();

            if (name && password && phone) {
                $.ajax({
                    url: '/user/register', // 服务器处理注册请求的 URL
                    method: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        name: name,
                        pwd: password,
                        phone: phone
                    }),
                    success: function(response) {
                        if (response === "1") {
                            alert('注册成功！');
                        } else {
                            alert('注册失败，请检查输入或重试。');
                        }
                    },
                    error: function(xhr, status, error) {
                        alert('注册失败：' + error);
                    }
                });
            } else {
                alert('请填写所有字段。');
            }
        });
    });
</script>
</body>
</html>
